Rounding
Strategy to round a Double or Float value to a given precision scale using a specified java.math.RoundingMode policy. The companion object exposes two factory methods to create the appropriate strategy:
to: creates a PreciseRounding object, which rounds a value to a specific precision scale and by a specific rounding mode
no: creates a NoRounding object, which doesn't round a value
If you need to round a value, call the to factory method:
val rounding = Rounding.to(1, RoundingMode.UP)
rounding.round(5.76) // 5.8
Content copied to clipboard
To round up to the nearest power of ten, use a negative precision:
val rounding = Rounding.to(-1)
rounding.round(5555.55) // 5560.0 -- or, 5.56E3
Content copied to clipboard
See significant figures on Wikipedia for an arithmetic background.